<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/jsp/common/common_include_for_server.jsp" %>
		<link rel="STYLESHEET" type="text/css" href="<%=request.getContextPath() %>/js/dhtmlxTree/dhtmlxtree.css"></link>
		<script  src="<%=request.getContextPath() %>/js/dhtmlxTree/dhtmlxcommon.js"></script>
		<script  src="<%=request.getContextPath() %>/js/dhtmlxTree/dhtmlxtree.js"></script>
		<script type="text/javascript">
		<!--
			var saveRoleDialog = null;
			var configRoleMenuDialog = null;
			var menuTree = null;
			/* onload init method start.
			**************************************************************/
			$(function(){
				// init save user dialog:
				saveRoleDialog = $('#divSaveRoleDialog').dialog({
					title: '角色信息',
					autoOpen: false, 
					resizable: false,
					modal: true,
					open: function(event, ui) {
						var roleId = $('#saveRoleForm input[name="role.roleId"]').val();
						if(roleId.length == 0){
							saveRoleDialog.dialog('option', 'title', '添加新角色');
						}else{
							saveRoleDialog.dialog('option', 'title', '修改角色信息');
						}
					},
					buttons: {
						"确定":function(){
							$.post('<%=request.getContextPath() %>/manage/userAction!saveRoleDo.action', 
								$('#saveRoleForm').serialize(),
								function(){
									window.location.href = window.location.href;
							});
						},
						"取消":function(){ saveRoleDialog.dialog('close'); }
					}
				});
				// init divConfigRoleMenuDialog:
				configRoleMenuDialog = $('#divConfigRoleMenuDialog').dialog({
					title: '角色信息',
					autoOpen: false, 
					resizable: false,
					modal: true,
					buttons: {
						"确定":function(){
							$('#roleMenuForm input[name="menuId"]').val(menuTree.getAllChecked());
							$.post('<%=request.getContextPath() %>/manage/userAction!configRoleMenuDo.action', 
								$('#roleMenuForm').serialize(),
								function(){
									alert('操作成功！');
									configRoleMenuDialog.dialog('close');
							});
						},
						"取消":function(){ configRoleMenuDialog.dialog('close'); }
					}
				});
				//DhtmlxTree:
				var menuTreeXmlStr = "${menuTreeXmlStr}";
				menuTree = new dhtmlXTreeObject("menuTreeDiv","100%","100%",0);
				menuTree.setSkin('dhx_skyblue');
				menuTree.setImagePath("<%=request.getContextPath() %>/js/dhtmlxTree/imgs/csh_bluebooks/");
				menuTree.enableCheckBoxes(1);
				menuTree.enableThreeStateCheckboxes(true);
				menuTree.loadXMLString(menuTreeXmlStr, function(){  });
			});
			
			/* onload init method end.
			**************************************************************/
			function initDialog4AddRole(){
				$('#saveRoleForm input[name="role.roleId"]').val('');
				$('#saveRoleForm input[name="role.roleName"]').val('');
				saveRoleDialog.dialog('open');
			}
			function initDialog4UpdateRole(roleId, roleName){
				$('#saveRoleForm input[name="role.roleId"]').val(roleId);
				$('#saveRoleForm input[name="role.roleName"]').val(roleName);
				saveRoleDialog.dialog('open');
			}
			function initDialog4ConfigRoleMenus(roleId, roleName){
				menuTree.setSubChecked(0,false);
				$.post('<%=request.getContextPath()%>/manage/userAction!findLeafMenuIdsByRole.action?role.roleId='+ roleId, function(data){
					if(typeof(data)==='object' && typeof(data.menuId)==='object'){
						$('#roleMenuForm input[name="role.roleId"]').val(roleId);
						$('#roleMenuForm span.role_name').text(roleName);
						var userMenuIds = data.menuId;
						$('#roleMenuForm input[name="roleId"]').attr('checked', false);
						if(userMenuIds && userMenuIds.length>0){
							for(var i=0; i<userMenuIds.length; i++){
								menuTree.setCheck(userMenuIds[i], true);
							}
						}
					}
					configRoleMenuDialog.dialog('open');
				});
			}
			function deleteUser(userId){
				if(confirm('确定删除该用户？')){
					$('#query_form input[name="user.userId"]').val(userId);
					$('#query_form').attr('action', '<%=request.getContextPath()%>/manage/userAction!deleteUserDo.action');
					$('#query_form').submit();
				}
			}
		//-->
		</script>
	<!-- Dialogs start -->
	<div id="divSaveRoleDialog" style="display: none;">
	    <s:form id="saveRoleForm">
	    	<input type="hidden" name="role.roleId"/>
	    	角色名称：<input type="text" name="role.roleName" />
	    </s:form>
	</div>
	<div id="divConfigRoleMenuDialog" style="display: none;">
		<form id="roleMenuForm">
			<input type="hidden" name="role.roleId"/>
			<input type="hidden" name="menuId"/>
	    	角色名称：<b><span class="role_name"></span></b>
			<div id="menuTreeDiv" class="jsTree" style="height: 300px;"></div>
		</form>
	</div>
	
	<!-- saveRoleDialog -->
	<input type="button" value="添加角色" onclick="initDialog4AddRole();"/>
	<table border="1">
		<tr>
			<th>ID</th>
			<th>角色名名</th>
			<th>操作</th>
		</tr>
		<s:iterator value="roleList">
			<tr id="user_tr_<s:property value="roleId"/>">
				<td><s:property value="roleId"/></td>
				<td><s:property value="roleName"/></td>
				<td>
					<a href='javascript:initDialog4ConfigRoleMenus("<s:property value='roleId'/>", "<s:property value='roleName'/>");'>分配权限</a>
					<a href='javascript:initDialog4UpdateRole("<s:property value='roleId'/>", "<s:property value='roleName'/>");'>修改信息</a>
					<a href='javascript:deleteUser("<s:property value='roleId'/>")'>删除</a>
				</td>
			</tr>
		</s:iterator>
	</table>
